package org.mifos.boss.util;

import java.io.IOException;

import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.Logger;
import org.mifos.boss.definitions.customerregistration.Mobile2BossDataExchange;
import org.mifos.boss.domainmodel.Customer;
import org.mifos.boss.domainmodel.MobileFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.XmlMappingException;
import org.springframework.stereotype.Repository;

@Repository(value="parser")
public class BossUnmarshallerUtil {

	protected static Logger logger = Logger.getLogger(BossUnmarshallerUtil.class);

	@Autowired
	@Qualifier("unmarshaller")
	private Unmarshaller unmarshaller;

	public Mobile2BossDataExchange clientDataParser(MobileFileUpload mobileFileUpload) throws IOException{
		Mobile2BossDataExchange clientData = null;

		try {
			clientData = (Mobile2BossDataExchange) unmarshaller.unmarshal(new StreamSource(mobileFileUpload.getFileData().getInputStream()));
		} catch (XmlMappingException e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			throw e;
		} catch (IOException e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			throw e;
		}

		logger.debug(clientData.getTransferTimestamp());
		/*for (ClientCreationalDetailType c : clientData.getClientCreationalType()) {
			logger.debug(c.getClientName());
		}*/
		return clientData;

	}
}
